package com.eud.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.eud.dao.MovieDao;
import com.eud.dao.impl.MovieDaoImpl;
import com.eud.entity.Movie;
import com.eud.entity.User;
import com.eud.service.MovieService;
import com.eud.service.UserService;
import com.eud.service.impl.MovieServiceImpl;
import com.eud.service.impl.UserServiceImpl;

/**
 * Servlet implementation class LogServlet
 */
@WebServlet("/LogServlet")
public class LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		HttpServletRequest hrequest = (HttpServletRequest)request;
		String url = hrequest.getRequestURL().toString();
		UserService s=new UserServiceImpl();
		User u=s.query(username);
		List add=s.queryAdminAll();
		
		if(!("".equals(username)) && !("".equals(userpass)) && (u!=null) && userpass.equals(u.getuPass()) ) {
			HttpSession session = request.getSession();
			session.setAttribute("user", username);
			MovieService m=new 	MovieServiceImpl();
			MovieDao md=new MovieDaoImpl();
			List<Movie> l=m.queryF();
			List<Movie> l2=m.queryFs();
			List<Movie> l3=m.queryByp();
			List<Movie> l4=md.queryF4();
			Map<String,List<Movie>> map=new HashMap<String,List<Movie>>();
			map.put("l",l);
			map.put("l2",l2);
			map.put("l3",l3);
			map.put("lm",l4);
			session.setAttribute("map", map);
	
			request.getRequestDispatcher("index.jsp").forward(request, response);
			
		}else {
			request.setAttribute("mes", "登录失败，用户名或密码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		
		
	}
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
